package com.hitqz.robot.biz.flow.component.agv;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolPreAndEndTicketsDto;
import com.hitqz.robot.api.common.entity.Robot;
import com.hitqz.robot.biz.flow.domain.constant.FlowConstant;
import com.hitqz.robot.biz.flow.util.LiteFlowUtil;
import com.hitqz.robot.biz.service.RobotMapPosService;
import com.hitqz.robot.biz.service.RobotService;
import com.hitqz.robot.driver.parent.RobotDriverService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import java.util.Optional;

/**
 * @author xupkun
 * @date 2024/11/6
 */
@Slf4j
@LiteflowComponent(value = "AgvStorage", name = "[轮对/轮轴]存放完成")
@AllArgsConstructor
public class AgvStorage extends NodeComponent {

    private final RobotDriverService robotDriverService;

    private final RobotMapPosService robotMapPosService;

    private final RobotService robotService;
    private final RestTemplate restTemplate;


    @Override
    public void process() throws Exception {
        LiteFlowUtil.checkInterruptFlag();
        FlowContextDto dto =  this.getRequestData();
        Robot robot = robotService.getDefaultRobot();
        FlowPatrolPreAndEndTicketsDto ticketsDto = JSONObject.parseObject(dto.getTicketsTask()
                .getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
        String source = Optional.ofNullable(ticketsDto.getModalData().get("sourcePosName"))
                .orElse(ticketsDto.getModalData().get("sourcePosCode")).toString();
        String targetName = Optional.ofNullable(ticketsDto.getModalData().get("targetPosName"))
                .orElse(ticketsDto.getModalData().get("targetPosCode")).toString();

        String auto = ticketsDto.getModalData().get("auto")== null ? "0":
                String.valueOf(ticketsDto.getModalData().get("auto"));

        if (auto.equals("0")) {
            log.info("半自动任务:");
            return ;
        }
        log.info("通知存放完成 {}", targetName);
        if(targetName.contains("LM")) {
            targetName = "911017";
        }
        if (source.contains("LM")) {
            source = "911017";
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.valueOf("application/json;UTF-8"));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("sbID", robot.getRobotSn());
        jsonObject.put("des", targetName);
        jsonObject.put("src", source);
        jsonObject.put("srcnum", Integer.parseInt((String) ticketsDto.getModalData().get("srcnum")));

        jsonObject.put("data", JSONArray.parse(String.valueOf(ticketsDto.getModalData().get("pjData"))));
        try {
            String key = FlowConstant.NODE_CUSTOM_INFO_MAP.get(this.getNodeId()).getParamNames().get(0);
            String uri = this.getCmpData(cn.hutool.json.JSONObject.class).get(key, String.class);
            log.info("store url:" + uri);
            HttpEntity<String> strEntity = new HttpEntity<String>(jsonObject.toString(), headers);
            log.info("缓存完成接口:" + strEntity.toString());
            // "http://172.24.0.191:8090/api/v1/centreApp/agv/storage/"
            String result = restTemplate.postForObject(uri,
                    strEntity, String.class);
            log.info("reset session" + result);
        } catch (Exception e) {

        }




    }
}
